<template>
	<view>
		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog title="完善收货地址" @close="close" @confirm="confirm">
				<uni-forms ref="form">
					<uni-forms-item label="姓名" name="" required="true">
						<uni-easyinput v-model="user.name" placeholder="" />
					</uni-forms-item>
					<uni-forms-item label="手机号码" name="" required="true">
						<uni-easyinput type="number" v-model="user.phone" placeholder="" />
					</uni-forms-item>
					<uni-forms-item label="收货地址" name="" required="true">
						<uni-easyinput type="textarea" v-model="user.address" placeholder="" />
					</uni-forms-item>
				</uni-forms>
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: "",
					phone: "",
					address: ""
				},
				id: "",
			}
		},
		methods: {
			confirm() {
				if (this.user.name.length == 0 || this.user.phone.length != 11 || this.user.address.length == 0) {
					uni.showToast({
						title: "请正确填写收货信息",
						icon: 'none'
					})
					// setTimeout(() => {
					uni.navigateBack({
						delta: 1
					})
					// }, 500)
					return
				}
				console.log("xxx");
				uni.request({
					url: this.api.updateAddress,
					method: 'POST',
					data: {
						orderId: this.id,
						name: this.user.name,
						phone: this.user.phone,
						address: this.user.address
					},
					success: res => {
						uni.setStorageSync('address', this.user)
						if (res.data.code == 200) {
							uni.reLaunch({
								url: "/pages/personal/personal"
							})
						}
						else{
							uni.showToast({
								title:res.data.message
							})
						}
					},
					fail: (err) => {
						console.log(err);
					},
					complete: () => {}
				});
			},
			close() {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			}
		},
		onLoad(open) {
			console.log(open.OrderId);
			this.id = open.OrderId
			if (uni.getStorageSync("address")) {
				this.user = uni.getStorageSync("address")
			}
			
		},
		onReady() {
			this.$refs.popup.open()
			console.log("下来");
		},
		onLaunch() {
			this.$refs.popup.open()
		}
	}
</script>

<style>

</style>
